/*
 * Copyright (C) 2019 ~ 2021 UnionTech Software Technology Co., Ltd.
 *
 * Author:     caitao <caitao@uniontech.com>
 *
 * Maintainer: caitao <caitao@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "cchartwidget.h"

#include <DGuiApplicationHelper>

#include <DApplicationHelper>
#include <DLabel>
#include <QDebug>
#include <QPaintEvent>
#include <QPainter>
#include <QVBoxLayout>

DGUI_USE_NAMESPACE

CChartWidget::CChartWidget(QWidget *parent)
    : QWidget(parent)
{
    initUi();
}

CChartWidget::~CChartWidget()
{
}

void CChartWidget::initUi()
{
    m_xAxisItemTextList = {"1", "2", "3", "4",
                                     "5", "6", "7", "8",
                                     "9", "10", "11", "12",
                                     "13", "14", "15", "16",
                                     "17", "18", "19", "20",
                                     "21", "22", "23", "24"};
    m_xItemTopTextList = {"1", "2", "3", "4",
                                     "5", "6", "7", "8",
                                     "9", "10", "11", "12",
                                     "13", "14", "15", "16",
                                     "17", "18", "19", "20",
                                     "21", "22", "23", "24"};
    m_xItemValueList = {12313.1, 1231.0, 666666.8, 89009.0,
                                     12313.1, 1231.0, 666666.8, 89009.0,
                                     12313.1, 1231.0, 666666.8, 89009.0,
                                     12313.1, 1231.0, 666666.8, 89009.0,
                                     12313.1, 1231.0, 666666.8, 89009.0,
                                     12313.1, 1231.0, 666666.8, 89009.0};
    m_xAxisDescText = "时间/h"; // x轴说明

    m_yAxisItemTextList = {"1", "2", "3", "4", "555555555"};
    m_yMaxValue = 666666.8 * 1.2;
    m_yMinValue = 0.0;
    m_yAxisDescText = "流量/MB"; // y轴说明
    m_xItemRectColor = Qt::GlobalColor::blue;

    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);

    m_label = new DLabel(this);
    m_label->setVisible(false);
}

void CChartWidget::setXAxisItemTextList(const QList<QString> &list)
{
    m_xAxisItemTextList = list;
}

void CChartWidget::setXItemTopTextList(const QList<QString> &list)
{
    m_xItemTopTextList = list;
}

void CChartWidget::setXItemValueList(const QList<qreal> &list)
{
   m_xItemValueList = list;
}

void CChartWidget::setXItemRectColor(const QColor &color)
{
    m_xItemRectColor = color;
}

void CChartWidget::setXAxisDescText(const QString &desc)
{
    m_xAxisDescText = desc;
}

void CChartWidget::setYAxisItemTextList(const QList<QString> &list)
{
    m_yAxisItemTextList = list;
}

void CChartWidget::setYMaxValue(qreal value)
{
    m_yMaxValue = value;
}

void CChartWidget::setYMinValue(qreal value)
{
    m_yMinValue = value;
}

void CChartWidget::setYAxisDescText(const QString &desc)
{
    m_yAxisDescText = desc;
}

void CChartWidget::paintEvent(QPaintEvent *e)
{
    QWidget::paintEvent(e);

    QRect rect = e->rect();
    QPainter painter(this);
    painter.setRenderHint(QPainter::RenderHint::Antialiasing, true);
    // 获取字体颜色
    DPalette pa = DApplicationHelper::instance()->palette(m_label);
    QColor fontColor = pa.color(DPalette::ColorRole::Text);

    // y轴说明宽、高
    int yAxisDescTextWidth = fontMetrics().horizontalAdvance(m_yAxisDescText);
    int yAxisDescTextHeight = m_yAxisDescText.isEmpty() ? 0 : fontMetrics().height();
    // x轴说明宽、高
    int xAxisDescTextWidth = fontMetrics().horizontalAdvance(m_xAxisDescText);
    int xAxisDescTextHeight = m_xAxisDescText.isEmpty() ? 0 : fontMetrics().height();
    int xAxisDescToXAxisSpace = 5;

    // 计算y轴标签最大宽度
    int yLabelMaxWidth = 0;
    for (const QString &yText : m_yAxisItemTextList) {
        int yTextWidth = fontMetrics().horizontalAdvance(yText);
        if (yLabelMaxWidth < yTextWidth) {
            yLabelMaxWidth =  yTextWidth;
        }
    }

    // 画y轴内容
    painter.save();
    // 画y轴说明
    QRect yAxisDescTextRect(rect.left() + yAxisDescTextWidth / 2, rect.top(), yLabelMaxWidth, yAxisDescTextHeight);
    painter.setPen(QPen(fontColor, 2));
    painter.drawText(yAxisDescTextRect, Qt::AlignVCenter | Qt::AlignRight, m_yAxisDescText);

    // 画y轴上标签
    int yLabelItemLenth = 10;
    int xLabelToXAxisSpace = 5; // x轴标签到x轴的间隙
    int yLabelHeightIndex = rect.top() + yAxisDescTextHeight;
    if (!m_yAxisItemTextList.isEmpty()) {
        yLabelItemLenth = (rect.height() - xLabelToXAxisSpace - fontMetrics().height() - yAxisDescTextHeight) / m_yAxisItemTextList.size();
    }
    int yLabelToYAxisSpace = 5; // 标签到y轴的间隙
    for (const QString &yText : m_yAxisItemTextList) {
        int yTextHeight = fontMetrics().height();
        // 画标签文字
        painter.setPen(QPen(fontColor, 2));
        QRect textRect(rect.left(), yLabelHeightIndex, yLabelMaxWidth, yTextHeight);
        painter.drawText(textRect, Qt::AlignVCenter | Qt::AlignRight, yText);
        // 画刻度线
        QPen linePen;
        linePen.setDashPattern({3, 4, 3, 4});
        linePen.setColor(fontColor);
        linePen.setWidth(2);
        painter.setPen(linePen);
        painter.drawLine(rect.left() + yLabelMaxWidth + yLabelToYAxisSpace, yLabelHeightIndex + yTextHeight / 2
                         , rect.right() - xAxisDescToXAxisSpace - xAxisDescTextWidth, yLabelHeightIndex + yTextHeight / 2);
        yLabelHeightIndex += yLabelItemLenth;
    }
    // 画y轴
    painter.setPen(QPen(fontColor, 2));
    painter.drawLine(rect.left() + yLabelMaxWidth + yLabelToYAxisSpace, rect.top() + yAxisDescTextHeight,
                     rect.left() + yLabelMaxWidth + yLabelToYAxisSpace,
                     rect.bottom() - xLabelToXAxisSpace - fontMetrics().height());
    painter.restore();

    /// 绘制x轴内容
    painter.save();
    // 绘制x轴上标签
    int xLabelItemLenth = 10;
    int xLabelWidthIndex = rect.left() + yLabelMaxWidth + yLabelToYAxisSpace;
    if (!m_xAxisItemTextList.isEmpty()) {
        double xLabelItemLenthDouble = (rect.width() - yLabelMaxWidth - yLabelToYAxisSpace
                           - xAxisDescToXAxisSpace - xAxisDescTextWidth)
                / (m_xAxisItemTextList.size() + 0.5);
        xLabelItemLenth = int(xLabelItemLenthDouble);
    }
    int xItemIndex = 0;
    m_xItemRectList.clear();
    for (const QString &xText : m_xAxisItemTextList) {
        int xTextHeight = fontMetrics().height();
        int xTextWidth = fontMetrics().horizontalAdvance(xText);
        xLabelWidthIndex += xLabelItemLenth;
        // 画标签文字
        painter.setPen(QPen(fontColor, 2));
        QRect textRect(xLabelWidthIndex - xTextWidth / 2, rect.bottom() - xLabelToXAxisSpace - xTextHeight,
                       xTextWidth, xTextHeight);
        painter.drawText(textRect, Qt::AlignVCenter | Qt::AlignHCenter, xText);

        // 画柱形
        painter.setPen(m_xItemRectColor);
        painter.setBrush(m_xItemRectColor);
        int xItemRectWidth = xLabelItemLenth / 2;
        int xItemRectMaxHeight = rect.height() - xTextHeight - xLabelToXAxisSpace;
        qreal xItemRectHeightPercent = (m_xItemValueList.at(xItemIndex) - m_yMinValue) / (m_yMaxValue - m_yMinValue);
        int xItemRectHeight = int(xItemRectMaxHeight * xItemRectHeightPercent);
        QRect itemRect(xLabelWidthIndex - xItemRectWidth / 2,
                       rect.bottom() - xTextHeight - xLabelToXAxisSpace - xItemRectHeight,
                       xItemRectWidth, xItemRectHeight);
        painter.drawRect(itemRect);
        m_xItemRectList.append(itemRect);
        // 画柱形顶部文字
        QString xItemTopText = m_xItemTopTextList.at(xItemIndex);
        int xItemTopTextWidth = fontMetrics().horizontalAdvance(xItemTopText);
        painter.setPen(QPen(fontColor, 2));
        QRect topTextRect(xLabelWidthIndex - xItemTopTextWidth / 2,
                          rect.bottom() - xTextHeight - xLabelToXAxisSpace - xItemRectHeight - xTextHeight - 5,
                          xItemTopTextWidth, xTextHeight);
        painter.drawText(topTextRect, Qt::AlignVCenter | Qt::AlignHCenter, xItemTopText);

        xItemIndex++;
    }
    // 画x轴
    painter.setPen(QPen(fontColor, 2));
    painter.drawLine(rect.left() + yLabelMaxWidth + yLabelToYAxisSpace,
                     rect.bottom() - xLabelToXAxisSpace - fontMetrics().height(),
                     rect.right() - xAxisDescToXAxisSpace - xAxisDescTextWidth, rect.bottom() - xLabelToXAxisSpace - fontMetrics().height());
    // 画x轴说明
    QRect xAxisDescTextRect(rect.right() - xAxisDescTextWidth,
                            rect.bottom() - xLabelToXAxisSpace - fontMetrics().height() - xAxisDescTextHeight / 2,
                            xAxisDescTextWidth, xAxisDescTextHeight);
    painter.setPen(QPen(fontColor, 2));
    painter.drawText(xAxisDescTextRect, Qt::AlignVCenter | Qt::AlignLeft, m_xAxisDescText);
    painter.restore();
}

void CChartWidget::mousePressEvent(QMouseEvent *e)
{
    for (int i = 0; i < m_xItemRectList.size(); ++i) {
        if (m_xItemRectList.at(i).contains(e->pos())) {
            qInfo() << Q_FUNC_INFO << i;
            Q_EMIT xItemClicked(i);
            break;
        }
    }

    QWidget::mousePressEvent(e);
}
